
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 *
 */

package com.hlkj.pay.app.token.ext.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.common.AdminUserResultCode;
import com.hlkj.pay.common.MerchantResultCode;
import com.hlkj.pay.dto.admin.AdminUserDto;
import com.hlkj.pay.enums.AdminUserEnum;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantInfoDO;
import com.hlkj.pay.infrastructure.model.token.TokenSecretDO;
import com.hlkj.pay.service.merchant.IMerchantService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/09/30 10:42
 */
@Slf4j
@Service
public class AdminLoginCommonCheck {

    @Autowired
    private IMerchantService merchantService;

    /**
     * OauthTokenResp
     * 
     * @return
     */
    public CommonResult<Void> checkUser(TokenSecretDO tenantSecretDO, AdminUserDto adminUserDto) {
        if (adminUserDto == null) {
            return CommonResult.error(AdminUserResultCode.USER_DOES_NOT_EXIST);
        }
        if (!AdminUserEnum.USER_STATUS.ABLE.getCode().equals(adminUserDto.getStatus())) {
            return CommonResult.error(AdminUserResultCode.TOKEN_USER_INFO_NOT_EFFECT);
        }
        if (!tenantSecretDO.getSysType().equals(adminUserDto.getSysType())) {
            return CommonResult.error(AdminUserResultCode.TOKEN_USER_INFO_SYS_TYPE_NOT_EFFECT);
        }
        if(StringUtils.hasText(adminUserDto.getSn())){
            MerchantInfoDO merchantInfoDO = merchantService.queryMerchant(adminUserDto.getSn());
            if(merchantInfoDO==null){
                return CommonResult.error(MerchantResultCode.MERCHANT_NOT_EXIST);
            }
            if(CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantInfoDO.getStatus())){
                return CommonResult.error(MerchantResultCode.MERCHANT_NOT_EFFECT);
            }
        }
        return CommonResult.success();
    }

}
